//
//  YDBaseOptionsViewController.m
//  PeiZiWangBei
//
//  Created by 巨彦新 on 2018/3/2.
//  Copyright © 2018年 LK-IOS. All rights reserved.
//

#import "YDBaseOptionsViewController.h"

#import "YDBuyOptionsViewController.h"
#import "YDInOptionsViewController.h"
#import "YDOutOptionsViewController.h"
#import "YDOptionsGuideController.h"//期权点买指引


@interface YDBaseOptionsViewController ()<UIScrollViewDelegate>


@property (nonatomic, strong)UIScrollView *scrollView; //滚动视图；
/**
 分段控制
 */
@property (nonatomic, strong)UISegmentedControl *segmentControl;

//顶部安全区域
@property (nonatomic , assign) CGFloat safeTopH;
//底部安全区域
@property (nonatomic , assign) CGFloat safeBottomH;
@end

@implementation YDBaseOptionsViewController


- (void)dealloc
{
    DDLog(@"===%@  Dealloc===",NSStringFromClass([self class]));
}
- (void)viewDidLoad
{
    [super viewDidLoad];

    [self setNav];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(buySuccess:) name:kOptionsBuySuccessNoti object:nil];
    [self addChildViewsController];
    
    [self.view addSubview:self.scrollView];
    
    [self addChildViewFromIndex:self.optionsType];
    [self refrshContentOffSet];
    
}

- (void)setNav
{
    self.view.backgroundColor = [UIColor colorWithHexString:mainGray];
    [self.navigationController.navigationBar.topItem setTitleView:self.segmentControl];
    
    self.safeTopH = isPhoneX ? 84 : 64;
    self.safeBottomH = self.isHiddenTabbar ? 0 : (isPhoneX ? 83 : 49);
    
    UIBarButtonItem *rightItemBtn = [UIBarButtonItem imageWithNormalImage:[UIImage imageNamed:@"h-guize"] hightImage:[UIImage imageNamed:@"h-guize"] addTarget:self action:@selector(rightItemButtonClick:)];
    self.navigationItem.rightBarButtonItem = rightItemBtn;
    
}
- (void)rightItemButtonClick:(UIButton *)sender
{
    YDOptionsGuideController *vc = [[YDOptionsGuideController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)buySuccess:(NSNotification *)noti
{
    //申购成功
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (![SYUserInformation sharedInstance].isLogin || self.scrollView == nil)return;
        self.optionsType = InOptions;
        [self.segmentControl setSelectedSegmentIndex:1];
        [self refrshContentOffSet];
        
    });
}


- (void)refrshContentOffSet
{
    switch (self.optionsType) {
        case BuyOptions:
            
            break;
        case InOptions:
            [self.scrollView setContentOffset:CGPointMake(LKScreenW, 0) animated:NO];
            break;
        case OutOptions:
            [self.scrollView setContentOffset:CGPointMake(LKScreenW * 2, 0) animated:NO];
            break;
        default:
            break;
    }
}
-(void)addChildViewsController
{
    
    YDBuyOptionsViewController *vc = [YDBuyOptionsViewController new];
    vc.isHiddenTabbar = self.isHiddenTabbar;
    [self addChildViewController:vc];
    
    
    YDInOptionsViewController *invc = [YDInOptionsViewController new];
    invc.isHiddenTabbar = self.isHiddenTabbar;
    [self addChildViewController:invc];
    
    YDOutOptionsViewController *outvc = [YDOutOptionsViewController new];
    outvc.isHiddenTabbar = self.isHiddenTabbar;
    [self addChildViewController:outvc];
}

- (void)segmentDidSelected:(UISegmentedControl *)segment
{
    NSInteger currentIndex =  segment.selectedSegmentIndex;
    
    
    if (currentIndex == 0) {
        self.optionsType = BuyOptions;
    }else if (currentIndex == 1){
        self.optionsType = InOptions;
    }else if (currentIndex == 2){
        self.optionsType = OutOptions;
    }
    
    if (!self.childViewControllers.count) {return;}
    
    
    [self.scrollView setContentOffset:CGPointMake(LKScreenW *currentIndex, 0) animated:YES];
    
    [self addChildViewFromIndex:currentIndex];
    
}

#pragma mark - UIScrollviewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == self.scrollView) {
        CGFloat offsetx = scrollView.contentOffset.x / LKScreenW;
        if (offsetx <= 0.5) {
            [self.segmentControl setSelectedSegmentIndex:0];
        }else if(offsetx <= 1)
        {
            [self.segmentControl setSelectedSegmentIndex:1];
        }else
        {
            [self.segmentControl setSelectedSegmentIndex:2];
        }
        
        [self addChildViewFromIndex:@(offsetx).integerValue];
        
        if (offsetx > 0.5) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"hideKeyBoard" object:nil];
        }
    }
}


/**
 添加自控制的view到scrollview
 */
- (void)addChildViewFromIndex:(NSInteger)index
{
    UIViewController *targetvc = [self.childViewControllers objectAtIndex:index];
    
    // 如果已经加载过，就不再加载
    if ([targetvc isViewLoaded]) return;
    
    targetvc.view.frame = CGRectMake(LKScreenW*index, 0, LKScreenW,self.scrollView.height);
    [self.scrollView addSubview:targetvc.view];
}


#pragma mark - getter
- (UIScrollView *)scrollView
{
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.showsHorizontalScrollIndicator = NO;
        
        _scrollView.frame = CGRectMake(0, 0, LKScreenW, LKScreenH - self.safeTopH - self.safeBottomH);
        _scrollView.delegate = self;
        _scrollView.contentSize = CGSizeMake(LKScreenW *3, 0);
        _scrollView.pagingEnabled = YES;
    }
    
    return _scrollView;
}
-(UISegmentedControl *)segmentControl
{
    if (_segmentControl == nil) {
        NSArray *titleArr = @[@"申购",@"持仓",@"结算"];
        
        UISegmentedControl *segmentControl = [[UISegmentedControl alloc] initWithItems:titleArr];
        
        segmentControl.frame =CGRectMake(0,0,rate375(260), 26);
        [segmentControl setSelectedSegmentIndex:0];
        segmentControl.tintColor = [UIColor whiteColor];
        [segmentControl addTarget:self action:@selector(segmentDidSelected:) forControlEvents:UIControlEventValueChanged];
        [segmentControl setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor colorWithWhite:1 alpha:0.6],
                                                 NSFontAttributeName:[UIFont systemFontOfSize:16]
                                                 }
                                      forState:UIControlStateNormal];
        
        [segmentControl setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor colorWithHexString:@"0E4483"],
                                                 NSFontAttributeName:[UIFont systemFontOfSize:16]}
                                      forState:UIControlStateSelected];
        
        _segmentControl = segmentControl;
    }
    return _segmentControl;
}




@end
